<?php

/**
 * Controller_graduate
 */

class Controller_graduate extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->grade    		= YOU::loadModel('grade');
		$this->model->class    		= YOU::loadModel('class');
		$this->model->subject    	= YOU::loadModel('subject');
		$this->model->semester 		= YOU::loadModel('semester');
		$this->model->excel 		= YOU::loadModel('excel');
	}
	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}
	/** 毕业撤回列表 **/
	public function actionGraduateSetList()
	{
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_grade_up` WHERE upg_graduate ='1'");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$grade_lists = $this->db->fetchAll("SELECT * FROM `tbl_grade_up` WHERE upg_graduate ='1' ORDER BY upg_id LIMIT {$start}, ".PAGE_LIMIT."");

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("grade_lists", $grade_lists);
		$this->tpl->display('grade/graduate.set.list.tpl.php');
	}
	/** 毕业年級撤回 **/
	public function actionRecall()
	{
		$grade_id	=      $this->get->grade_id;
		$row = $this->model->grade->getNoRecall($grade_id);

		if (!empty($row)) {
			Helper_Client::alert("已经不能撤回");
		}else{
			$this->db->query("DELETE  FROM `tbl_grade_up` WHERE grade_id='{$grade_id}' AND upg_graduate='1'");
			// 查找刚毕业的班级
			$class_lists = $this->model->class->getGraduateNew($grade_id);
			foreach ($class_lists as $key => $value){

				$info = array(
				"is_now"   		=> 1,
				);
				$this->db->do_update("tbl_class_up", $info, "upc_id='{$value['upc_fid']}'");
			}
			// 写入数据库
			$info = array(
			"is_now"   		=> 1,
			);
			$this->db->do_update("tbl_grade_up", $info, "grade_id='{$grade_id}' AND is_now ='0'");
			$this->db->query("DELETE  FROM `tbl_class_up` WHERE grade_id='{$grade_id}' AND upc_graduate='1'");
			// 跳转
			Helper_Client::location("撤回成功", "index.php?a=graduate.graduatesetlist");
		}

	}
	/** ------------------------------------畢業學科------------------------------- **/
	public function actionGraduateSubjectList()
	{

		// 獲得當年畢業所有年級
		$time = date('Y',time());
		$class_lists 	= $this->model->class->getGraduateAll($time);
		// 全部学科
		$subject_lists 	= $this->model->subject->getall();
		if ($_POST) {
			foreach ($class_lists as $key=>$value){
				$class_lists[$key]['subject_ids'] = unserialize($value['subject_ids']);
				$student_name	= $_POST["subjcet_{$value['class_id']}"];
				$subject_name = serialize($student_name);
				if ($subject_name !='N;') {
					// 写入数据库
					$info = array(
					"subject_ids"   		=> $subject_name,
					);

					$this->db->do_update("tbl_class_up", $info, "class_id='{$value['class_id']}' AND upc_graduate ='1'");
				}

			}
			// 跳转
			Helper_Client::location("添加成功", "index.php?a=graduate.graduatesubjectlist");

		}

		$this->tpl->assign("subject_lists", $subject_lists);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->display('grade/graduate.subject.list.tpl.php');
	}
	/** ------------------------------------畢業登記------------------------------- **/
	/** 毕业登记列表 **/
	public function actionGraduateList()
	{
		$class_id			= $this->get->class_id;
		$graduate_year		= $this->get->graduate_year;
		if (!empty($class_id)) {
			$graduate_name			= $this->get->graduate_name;
			$graduate_num			= $this->get->graduate_num;
			// 毕业班级
			$class_data = $this->model->class->getGraduateinfo($class_id);
			$class_data['subject_ids'] = unserialize($class_data['subject_ids']);
			// 获得学科名称
			foreach ($class_data['subject_ids'] as $key => $value){

				$row = $this->model->subject->getinfo($value);
				$class_data['subject_name'][$row['subject_id']] = $row['subject_name'];

			}
			$where = "WHERE 1=1";
			if (!empty($class_id)) {
				$where .= " AND class_id ='{$class_id}' ";
			}
			if (!empty($graduate_name)) {
				$where .= " AND graduate_name LIKE '%{$graduate_name}%' ";
				$this->tpl->assign("graduate_name", $graduate_name);
			}
			if (!empty($graduate_num)) {
				$where .= " AND graduate_num ='{$graduate_num}' ";
				$this->tpl->assign("graduate_num", $graduate_num);
			}

			// 分页
			YOU::loadLib('page', "", '0');
			$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_graduate` ");
			$count = $tmp['count'];
			$page_total = ceil($count/PAGE_LIMIT);
			// 当前页
			$p = intval($_GET['p']);
			if ($p == 0){
				$p  = 1;
			}
			if ($p > 1){
				$start = PAGE_LIMIT * ($p-1);
			}else{
				$start = 0;
			}


			$graduate_lists		= $this->db->fetchAll("SELECT * FROM `tbl_graduate` {$where}  ORDER BY graduate_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
			foreach ($graduate_lists as $key => $value){
				// 成绩
				$graduate_lists[$key]['graduate_subject'] = unserialize($value['graduate_subject']);

			}

			$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
			$page->next_page   = '下一页>';
			$page->pre_page    = '<上一页';
			$page->style_path  = "static/page_style";
			$this->tpl->assign("page", $page->show(4, "yahoo"));
		}
		$this->tpl->assign("class_data", $class_data);
		$this->tpl->assign("class_id", $class_id);
		$this->tpl->assign("graduate_lists", $graduate_lists);
		$this->tpl->display('grade/graduate.list.tpl.php');
	}
	/** 毕业登记下载模板 **/
	public function actionGraduateDown()
	{
		// 毕业班级
		$time = date('Y',time());
		$class_lists = $this->model->class->getGraduateAll($time);
		if ($_POST) {
			$class_id 	= $this->post->class_id;

			// 查找本年级的学科
			$class_lists = $this->model->class->getGraduateinfo($class_id);
			$row = unserialize($class_lists['subject_ids']);
			$graduate_in['col'] = array("学号", "姓名", "学籍号", "毕业证书号", "毕业类型", "就读学校",);

			foreach ($row as $key => $value){
				// 获得学科名称
				$r = $this->model->subject->getinfo($value);
				$subject_name = $r['subject_name'];
				$graduate_in['col'][] = $subject_name;

			}

			$graduate_in['col'][] = "总分";
			$graduate_in['col'][] = "毕业评语";
			$graduate_in['col'][] = "备注";

			$data_lists = array();

			$this->model->excel->excel_ini['graduate_in']['col'] = $graduate_in['col'];
			$this->model->excel->outData("graduate_in", $data_lists);


		}
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->display('grade/graduate.down.tpl.php');
	}
	/** 导入信息 **/
	public function actionIn()
	{
		// 毕业班级
		$time = date('Y',time());
		$class_lists = $this->model->class->getGraduateAll($time);
		if ($_POST) {

			$class_id   = (int)$this->post->class_id;
			$data = $this->model->excel->in('graduate_in');
			// 查找本年级的学科
			$class_lists = $this->model->class->getGraduateinfo($class_id);
			$row = unserialize($class_lists['subject_ids']);
			if(empty($data)){
				Helper_Client::alert("请上传文件");
			}

			foreach ($data as $key => $value){
				$info = array(

				"class_id"   			=> $class_id,
				"graduate_year"   		=> $time,
				"graduate_no"   		=> $value[1],
				"graduate_name"   		=> $value[2],
				"graduate_num"   		=> $value[3],
				"graduate_certificate"  => $value[4],
				"graduate_type"   		=> $value[5],
				"graduate_school"   	=> $value[6],
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",
				);

				//  学科
				$score = array();
				foreach ($row as $k => $v){
					$ke = 7+$k;

					$score[$v] = $value[$ke];
				}

				$score = serialize($score);
				$info['graduate_subject'] = $score;

				$num =count($row)+7;

				$info['graduate_count'] 	= $value[$num];
				$info['graduate_content'] 	= $value[$num+1];
				$info['graduate_remark'] 	= $value[$num+2];



			}
			$this->db->do_insert("tbl_graduate", $info);
			// 跳转
			Helper_Client::location("导入成功", "index.php?a=graduate.graduatelist&class_id={$class_id}");
		}

		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->display('grade/graduate.in.tpl.php');
	}
	/** 毕业信息修改 **/
	public function actionGraduateUpdate()
	{
		$graduate_id 	= $this->get->graduate_id;
		$class_id 		= $this->get->class_id;
		// 查找信息
		$graduate_data = $this->model->class->getGraduatein($graduate_id);
		$graduate_data['graduate_subject'] = unserialize($graduate_data['graduate_subject']);
		// 获得学科名称
		foreach ($graduate_data['graduate_subject'] as $key => $value){

			$row = $this->model->subject->getinfo($key);
			$graduate_data['subject_name'][$key]['subject_name'] = $row['subject_name'];
			$graduate_data['subject_name'][$key]['subject_score'] = $value;
		}

		if ($_POST) {

			$graduate_year 			= $this->post->graduate_year;
			$graduate_num 			= $this->post->graduate_num;
			$graduate_name 			= $this->post->graduate_name;
			$graduate_certificate 	= $this->post->graduate_certificate;
			$graduate_type 			= $this->post->graduate_type;
			$graduate_school 		= $this->post->graduate_school;
			$graduate_subject 		= $this->post->graduate_subject;
			$graduate_count 		= $this->post->graduate_count;
			$graduate_content 		= $this->post->graduate_content;
			$graduate_remark 		= $this->post->graduate_remark;
			$graduate_subject 		= serialize($graduate_subject);
			$info = array(

			"graduate_year"  			=> $graduate_year,
			"graduate_num"  			=> $graduate_num,
			"graduate_name"  			=> $graduate_name,
			"graduate_certificate"  	=> $graduate_certificate,
			"graduate_type"  			=> $graduate_type,
			"graduate_school"  			=> $graduate_school,
			"graduate_subject"  		=> $graduate_subject,
			"graduate_count"  			=> $graduate_count,
			"graduate_content"  		=> $graduate_content,
			"graduate_remark"  			=> $graduate_remark,
			"update_time"           => date("Y-m-d H:i:s"),


			);
			$this->db->do_update("tbl_graduate", $info, "graduate_id='{$graduate_id}'");
			// 跳转
			Helper_Client::location("修改成功", "index.php?a=graduate.GraduateList&class_id={$class_id}");
		}
		$this->tpl->assign("graduate_data", $graduate_data);
		$this->tpl->assign("class_data", $class_data);
		$this->tpl->display('grade/graduate.update.tpl.php');
	}
	/** 毕业信息删除 **/
	public function actionGraduateDel()
	{
		$graduate_id 	= $this->get->graduate_id;
		$class_id 		= $this->get->class_id;

		$this->db->query("DELETE  FROM `tbl_graduate` WHERE graduate_id='{$graduate_id}'");
		// 跳转
		Helper_Client::location("删除成功", "index.php?a=graduate.GraduateList&class_id={$class_id}");
	}
	/** -------------------------毕业情况汇总----------------------------------- **/

	/** 毕业登记列表 **/
	public function actionGraduateCountList()
	{
		// 不重复的学年
		$year_lists = $this->model->class->getYearAll();

		$graduate_year	= $this->get->graduate_year;
		$where = " WHERE 1=1";
		if (!empty($graduate_year)) {
			$where .= " AND graduate_year='{$graduate_year}' ";
			$this->tpl->assign("graduate_year", $graduate_year);
		}else {
			$graduate_year = date('Y',time());
			$where .= " AND graduate_year='{$graduate_year}' ";
			$this->tpl->assign("graduate_year", $graduate_year);
		}
		$up = $graduate_year-1;

		// 当前学年的人数
		$graduate_count = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_graduate` {$where} ");

		// 上一学年学生人数
		$graduate_up = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_graduate` WHERE graduate_year = $up");
		// 学年人数差值
		$eq = $graduate_count['count']-$graduate_up['count'];
		// 当前学生总分
		$graduate_lists = $this->db->fetchAll("SELECT * FROM `tbl_graduate` {$where} ");
		foreach ($graduate_lists as $key => $value){
			$count_score += $value['graduate_count'];
		}
		// 上一学年学生总分
		$graduate_lists_up = $this->db->fetchAll("SELECT * FROM `tbl_graduate` WHERE graduate_year = $up ");
		foreach ($graduate_lists_up as $key => $value){
			$count_score_up += $value['graduate_count'];
		}
		// 总分值差值
		$eq_score = $count_score -$count_score_up;

		$this->tpl->assign("graduate_count", $graduate_count);
		$this->tpl->assign("graduate_up", $graduate_up);
		$this->tpl->assign("eq", $eq);

		$this->tpl->assign("count_score", $count_score);
		$this->tpl->assign("count_score_up", $count_score_up);
		$this->tpl->assign("eq_score", $eq_score);

		$this->tpl->assign("year_lists", $year_lists);
		$this->tpl->display('grade/m.graduate.content.tpl.php');
	}
	/** -------------------------毕业历史查询----------------------------------- **/
	/** 列表 **/
	public function actionHistory()
	{

		// 搜索
		$graduate_name	= $this->get->graduate_name;
		$graduate_num	= (int)$this->get->graduate_num;
	
		$where = "WHERE 1=1";

		if (!empty($graduate_name)){
			$where .= " AND graduate_name LIKE '%{$graduate_name}%' ";
			$this->tpl->assign("graduate_name", $graduate_name);
		}
		if (!empty($graduate_num)){
			$where .= " AND graduate_num ={$graduate_num} ";
			$this->tpl->assign("graduate_num", $graduate_num);
		}
		if ($where !="WHERE 1=1") {
			$graduate_lists = $this->db->fetchAll("SELECT * FROM `tbl_graduate` {$where} ORDER BY graduate_id DESC ");
		}
		if(is_array($graduate_lists)){
            foreach ($graduate_lists as $key => $value){
                // 成绩
                $graduate_lists[$key]['graduate_subject'] = unserialize($value['graduate_subject']);

                foreach ($graduate_lists[$key]['graduate_subject'] as $k =>$v){
                    $row = $this->model->subject->getinfo($k);
                    $graduate_lists[$key]['graduate_na'][$row['subject_name']] =$v ;
                }

            }
        }


		$this->tpl->assign("graduate_lists", $graduate_lists);
		$this->tpl->assign("grade_lists", $grade_lists);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("class_data", $class_data);

		$this->tpl->display('grade/m.graduate.history.tpl.php');
	}

}
?>